package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TCircleName;
import com.quwan.manager.controller.dto.CircleNameDto;
import com.quwan.manager.service.ICircleNameService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laozhang
 * @date 2022/3/17
 */
@Api(tags = "后台圈子名称管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/circleName")
public class CircleNameController extends BaseController {

    private final ICircleNameService circleNameService;


    @ApiOperation(value = "圈子名称列表分页",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("circleNameListPage")
    public ApiResult<PageResult<CircleNameDto>> CircleNameListPage(@RequestParam(defaultValue = "1") int page,
                                                                   @RequestParam(defaultValue = "10") int size,
                                                                   @ApiParam(value="圈子类型id",required = false) @RequestParam(required = false) Long circleTypeId,
                                                                   @ApiParam(value="名称",required = false) @RequestParam(required = false) String circleName,
                                                                   @ApiParam(value="状态 1:正常,2:禁用",required = false) @RequestParam(required = false) Integer status) {
        PageResult<CircleNameDto> res = circleNameService.getCircleNameListPage(page, size, circleTypeId, circleName, status);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "圈子名称列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("circleNameList")
    public ApiResult<List<CircleNameDto>> CircleNameList(
            @ApiParam(value="圈子类型id",required = false) @RequestParam(required = false) Long circleTypeId,
            @ApiParam(value="名称",required = false) @RequestParam(required = false) String circleName,
            @ApiParam(value="状态 1:正常,2:禁用",required = false) @RequestParam(required = false) Integer status) {
        List<CircleNameDto> res = circleNameService.getCircleNameList(circleTypeId,circleName, status);
        return ApiResult.success(res);
    }

    @ApiOperation("圈子名称修改——不需要传时间字段")
    @PostMapping("updateCircleName")
    public ApiResult updateCircleName(HttpServletRequest request,TCircleName circleName){
        circleName.setLastModifyTime(LocalDateTime.now());
        boolean success = circleNameService.updateCircleName(circleName);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addCircleName")
    @ApiOperation("添加圈子名称——不需要传时间字段")
    public ApiResult addCircleName(HttpServletRequest request,TCircleName circleName){
        TokenInfo tokenInfo = getTokenInfo(request);
        circleName.setCreateTime(LocalDateTime.now());
        circleName.setCreateUserName(tokenInfo.getUsername());
        boolean success = circleNameService.addCircleName(circleName);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "圈子名称详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailCircleName")
    public ApiResult detailCircleName(@RequestParam(value = "id") Integer id) {
        TCircleName CircleName = circleNameService.detailCircleName(id);
        return ApiResult.success(CircleName);

    }

    @PostMapping("delCircleName")
    @ApiOperation("删除圈子名称")
    public ApiResult delCircleName(@RequestParam(value = "id") Integer id){
        boolean success = circleNameService.delCircleName(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }


}
